package com.example.springboot.service.impl;

import cn.hutool.core.date.DateTime;
import com.example.springboot.mapper.ValidationMapper;
import com.example.springboot.pojo.Validation;
import com.example.springboot.service.ValidationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;


@Service
public class ValidationServiceImpl implements ValidationService {

    @Autowired
    private ValidationMapper validationMapper;

    @Override
    public void saveCode(String email, String code, DateTime time) {
        validationMapper.saveCode(email,code,time);
    }

    @Override
    public Validation[] findValidation(String email) {
        return validationMapper.findValidation(email);
    }

    @Override
    public boolean checkValidation(String email, String code) {
        Validation v = findValidation(email)[0];
        String validation = v.getCode();
        Timestamp time = v.getTime();
        return validation.equals(code) && time.after(new Date());
    }

    @Override
    public void deleteValidation(String email) {
        validationMapper.deleteValidation(email);
    }
}
